<template>
  <!-- 导航栏 -->
  <van-nav-bar
    title="新增收货地址"
    left-text="返回"
    left-arrow
    @click-left="back"
  />

  <van-address-edit
    :area-list="areaList"
    :address-info="address"
    show-set-default
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
  />


  

</template>

<script setup>
  // 引入地区数据
  import { areaList } from '@vant/area-data';

  const {proxy} = getCurrentInstance()

  let business = proxy.$cookies.get('business') ? proxy.$cookies.get('business') : {}
  business = reactive(business)
  
  var area = business.region_text.split('/')
  var region = business.district || business.city || business.province

  let address = ref({
    name: business.nickname,
    tel: business.mobile,
    province: area[0],
    city:  area[1],
    county:  area[2],
    areaCode: region
  })
  
  const back = () => proxy.$router.go(-1)

  const onSave = async (values) =>
  {
    //组装数据
    var data = {
      busid: business.id,
      consignee: values.name,
      address: values.addressDetail,
      mobile: values.tel,
      status: values.isDefault ? '1' : '0',
      code: values.areaCode
    }

    var result = await proxy.$POST({
      url: '/address/add',
      params: data
    })

    if(result.code == 0)
    {
      proxy.$fail(result.msg)
      return false
    }else
    {
      proxy.$success({
        message: result.msg,
        onClose: () =>
        {
          proxy.$router.go(-1)
        }
      })
    }
  }

</script>


